package com.syht.wechatpay.tools;

import java.util.HashMap;
import java.util.Map;

/**
 * XML工具类
 * 
 * @author PWWang
 * 
 */
public class XmlTools {


	/**
	 * 封装请求数据
	 * 
	 * @param k
	 * @param v
	 * @return
	 * @throws Exception
	 */
	public static String appendRequestXml(String[] k, String[] v)
			throws Exception {

		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<root>");
		for (int i = 0; i < k.length; i++) {
			sb.append("<" + k[i] + ">" + v[i].trim() +"</" + k[i] + ">");
		}
		sb.append("</root>");
		return sb.toString();
	}

	/**
	 * 
	 * @param k
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> getXmlInfo(String[] k, String xml,
			String tradeType) throws Exception {

		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < k.length; i++) {
			if (xml.contains(k[i])) {
				String temp = xml.substring(xml.indexOf("<" + k[i] + ">")
						+ k[i].length() + 2, xml.indexOf("</" + k[i] + ">"));
				map.put(k[i], temp);
			}
		}
		return map;
	}

}
